<template>
  <div class="ycdwrap" id="ycdwrap">
    <div class="yc-table-wrp">
      <div>
        <yc-title  class="title" value="添加商品"></yc-title>
      </div>
      <el-form @submit.native.prevent :model="form" label-width="80px">
        <el-form-item label="商品名称:">
          <el-input class="input" maxlength="30" v-model.trim="form.good_name" style="width:20%; min-width: 270px;"></el-input>
        </el-form-item>
       <el-form-item label="商品图:">
        <el-upload class="avatar-uploader"
          accept="image/jpeg,image/gif,image/png,image/bmp"
          :headers="headers"
          :action="setUrl"
          ref="upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :on-change="handleUpload">
          <img v-if="form.imageUrl[0]"  :src="form.imageUrl[0]" class="avatar">
          <i v-else style="position: relative;z-index: 1;"  class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品编码:">
          <el-input maxlength="30"  class="input" v-model.number.trim="form.good_code" style="width:20%; min-width: 270px;" placeholder="请输入商品编码"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <yc-button   @click="handlePreservation" value="保存"></yc-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from 'request'
import ycTitle from '@/components/ycTitle.vue'
import ycButton from '@/components/button.vue'
import ycInput from '@/components/yc-input.vue'
import ycSearchButton from '@/components/searchButton.vue'
export default {
  name: 'addingGoods',
  components: {
    ycTitle,
    ycButton,
    ycSearchButton,
    ycInput
  },
  data () {
    return {
      active1: true,
      active2: false,
      form: {
        good_name: '',
        good_code: '',
        imageUrl: [],
        data: '',
        imageName: ''
      }
    }
  },
  methods: {
    handleDele () {
      this.form.imageUrl = []
    },
    handlePreservation (e) {
      let result
      if (this.form.good_code) {
        let patrn = /^\d+(\.\d+)?$/
        result = patrn.test(this.form.good_code)
        if (result === false) {
          this.$message.error('商品编码只能输入数值类型')
          return
        }
      }

      if (!this.form.good_name) {
        this.$message.error('请输入商品名称')
        return
      } else if (!this.form.good_code) {
        this.$message.error('请输入商品编码')
        return
      }
      if (!this.form.imageName) {
        this.$message.error('上传失败')
        return
      }

      var data = {
        goodCode: this.form.good_code,
        goodImage: this.form.imageName,
        goodName: this.form.good_name
      }
      request.uploadimg(data).then(res => {
        this.$message({
          message: '添加成功',
          type: 'success'
        })
        setTimeout(() => {
          this.$router.push('/goodsList')
        }, 300)
      }).catch(e => {
        this.$message.error('添加失败')
      })
      this.form.good_code = ''
      this.form.imageName = ''
      this.form.good_name = ''
      this.form.imageUrl = []
    },
    handleUpload (file, fileList) {
      this.form.data = file
      this.form.imageUrl[0] = URL.createObjectURL(file.raw)
      this.$forceUpdate()
    },
    handleAvatarSuccess (response, file, fileList) {
      this.form.imageName = response.fileName
    },
    beforeAvatarUpload (file) {
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('商品图片大小不能超过 10MB!')
        this.form.imageUrl = []
        this.form.imageName = ''
      }
      return isLt2M
    }
  },
  computed: {
    setUrl () {
      var url = process.env.BASE_ROOT + '/dealer/goods/uploadimg'
      return url
    },
    headers () {
      return {
        'Authorization': 'Bearer ' + localStorage.getItem('ycd_token')
      }
    }
  }
}
</script>

<style lang="stylus" scoped="scoped">
.yc-table-wrp{
  .title{
    margin 0 0 20px 0
  }
  .preservation{
    width 100px
    height 30px
    font-size 16px
  }
}
</style>
<style>
.avatar-uploader{
  display: inline-block;
}
.avatar-uploader .el-icon-label{
  position: absolute;
  top: -6px;
  right: -15px;
  display: block;
  width: 46px;
  height: 26px;
  background-color: #13ce66;
  transform: rotate(45deg);
  z-index: 99;
}
.avatar-uploader .el-icon-label .el-cion{
  position: absolute;
  transform: rotate(-45deg);
  top: 40%;
  right: 34%;
  color: #FFFFFF;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  margin: 0 10px 0 0;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>

<style scoped="scoped">
.avatar{
  width: 178px;
  height: 178px;
  display: block;
}
</style>
